﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowLevelGraphics.Filter
{
    public enum TONEMAPPING
    {
        Drago03,
        Reinhard,
        Fattal,
        Mantiuk,
        Reinhard_Devlin
    }

    public enum Operators
    {
        Biswas,
        Drago,
        Fattal,
        Linear,
        Reinhard05,
        Schlick,
        Stockham,
        Undefined
    }

    /// <summary>
    /// Tone Mapping
    /// </summary>
    public class ToneMapping : BaseImageFilter
    {
        protected Operator m_Operator = null;
        double gamma = 0;

        /// <summary>
        /// default constructor
        /// </summary>
        public ToneMapping()
        {
        }

        /// <summary>
        /// Gets or sets operator
        /// </summary>
        public Operator Operator
        {
            get { return m_Operator; }
            set { m_Operator = value; }
        }

        /// <summary>
        /// used to clone
        /// </summary>
        /// <param name="_toneMapping"></param>
        internal ToneMapping(ToneMapping _toneMapping)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            ArrayList pathsInput = new ArrayList();
            Operator tmo = null;
            bool createHDR = true;
            // load or create HDR image
            HDRFilter hdr = new HDRFilter();
            float[][,] input = null;

            if (createHDR)
            {

                Console.WriteLine("Creating HDR image...");
                input = hdr.CreateHDR((string[])pathsInput.ToArray(typeof(string)));
            }
            else
            {

                Console.WriteLine("Loading HDR image...");
                input = hdr.LoadHDR((string)pathsInput[0]);

                if (input == null)
                {

                    //ShowError("No input returned by the loader.");
                    //return;
                }
            }

            // tone map HDR image
            float[][,] output = null;

            if (tmo != null)
            {

                Console.WriteLine("Tonemapping HDR image...");
                tmo.Run(input, ref output);

                if (output == null)
                {

                    Console.WriteLine("No output returned by the operator.");
                    //return;
                }
            }
            else
            {

                output = input;
            }

            if (gamma != 1.0)
            {

                Console.WriteLine("Applying gamma correction...");
                hdr.ApplyGamma(output, gamma);
            }

            Console.WriteLine("Saving HDR image..."); return base.Execute(_bitmap);
        }

        /// <summary>
        /// Clones this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ToneMapping(this);
        }
    }
}
